const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
      page: 1,
      list: [],
      is_ajax: false,
      is_over: false,
      status: 10,
      user: {},

      showModalStatus:false   //弹出页面
  },
userinfo: function() {
    app.ajax({
        url: '/api/user/userinfo',
        success: res => {
            if (res.code == 1) {
                wx.setStorageSync('user', res.data);
                this.setData({
                    user: res.data
                })
            }
        }
    })
},
  unsettled:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/unsettled/unsettled?order_id=' + id
    })
  },
  opening_code: function (e) {
    var id = e.currentTarget.dataset.id;
    // pages/unsettled/unsettled
        wx.navigateTo({
      url: '/pages/unsettled/unsettled?order_id=' + id
    })
    // wx.navigateTo({
    //   url: '/pages/opening_code/opening_code?order_id=' + id
    // })
  },
  used: function () {
    wx.navigateTo({
      url: '/pages/used/used',
    })
  },
  expired: function () {
    wx.navigateTo({
      url: '/pages/expired/expired',
    })
  },
   //首页
   goindex: function(e) {
    wx.redirectTo({
        url: '/pages/index/index'
    })
},
   //我的订单
   myorder: function() {
    wx.redirectTo({
        url: '/pages/my_order/my_order'
    })
},
//我的
my: function() {
    wx.redirectTo({
        url: '/pages/my/my'
    })
},
  setStatus: function(e){
    console.log(e)
    var status = e.currentTarget.dataset.status;
    if(this.data.status != status){
      this.setData({
        status: status
      });
      this.getList(1);
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getList();
  },

  getList: function(reset){
    if(reset){
      this.setData({
        page: 1,
        list: [],
        is_ajax: false,
        is_over: false
      });
    }
    if(this.data.is_ajax || this.data.is_over) return false;
    this.setData({
      is_ajax: true
    });
    var page = this.data.page, status = this.data.status, list = this.data.list, is_ajax = true, is_over = false;
    app.ajax({
      url: '/api/user/orderList',
      data: {
        status: status,
        page: page
      },
      success: res => {
        if(res.code == 1){
          page++;
          list = list.concat(res.data);
          var now = parseInt(Date.now() / 1000);
          for(var i in list){
            if(list[i].status == 10){
              list[i].gopay_time = parseInt((list[i].create_time + 1800 - now) / 60);
              if(list[i].gopay_time < 0) list[i].gopay_time = 0;
            }
          }
        }else{
          is_over = true;
        }
        is_ajax = false;
        this.setData({
          page: page,
          list: list,
          is_ajax: is_ajax,
          is_over: is_over
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.userinfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  stores:function(e,ids){
    var id = e.currentTarget; 
    console.log(e.currentTarget.dataset.store_id,'e.currentTarget.dataset.store_id---------')
    wx.navigateTo({
        url: '/pages/opening_code/opening_code?order_id='+id
    })
  },
  store:function(e){
    var id = e.currentTarget.dataset.store_id;  
    var list = '';
    app.ajax({
      url: '/api/index/getRoomList',
      data: {
        store_id: id
      },
      success: res => {
        if(res.code == 1){
          list = res.data;
        }
        this.setData({
          RoomList:list,
          showModalStatus:true
        });
      }
    })

  },
  close:function(){
    this.setData({
      showModalStatus:false
    });
  },
  book:function (e) {
    var room_id = e.currentTarget.dataset.room_id;
    if(room_id)
      wx.navigateTo({
        url: '/pages/book/book?id=' +room_id,
      })
  }

  



})